package utils

import (
	"fmt"
	"strings"
	"time"
)

//获取特殊占位符内容
func GetTemplateKey(templateContent string, startStr string, endStr string) []string {
	//缓存
	resultKeys := make([]string, 0)
	startKeyIndex, endKeyIndex := 0, 0
	for index, val := range templateContent {
		val := fmt.Sprintf("%c", val)
		//判断
		if strings.EqualFold(startStr, val) {
			startKeyIndex = index
			continue
		}
		//判断
		if strings.EqualFold(endStr, val) {
			endKeyIndex = index
		}
		//获取
		if startKeyIndex != 0 && endKeyIndex != 0 {
			//获取中间的内容并且缓存
			keyVal := templateContent[startKeyIndex+1 : endKeyIndex]
			//缓存
			resultKeys = append(resultKeys, keyVal)
			//重置
			startKeyIndex, endKeyIndex = 0, 0
		}
	}
	//返回
	return resultKeys
}

func GetTimePoint(timeStr string) time.Time {
	//判断是否有效
	if len(timeStr) == 0 {
		return time.Now()
	} else {
		result, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
		if err != nil {
			return time.Now()
		} else {
			return result
		}
	}
}
